<?php

namespace App\Http\Controllers\Business;

use App\Events\AppointEvent;
use App\Events\Event;
use App\Events\ScheduleEvent;
use App\Model\Business\Appointment;
use App\Model\Business\ClasseStuden;
use App\Model\Business\Demoment;
use App\Model\Foundation\Student;
use App\Model\Foundation\TimeStock;
use App\Model\System\SystemConfig;
use Illuminate\Http\Request;
use App\Model\Foundation\Teacher;
use App\Model\System\Token;
use App\Model\Business\ScheduleStuden;
use Illuminate\Support\Facades\DB;
use Laravel\Lumen\Routing\Controller as BaseController;

use App\Model\Business\AppointmentSchedule;
use App\Model\Business\Schedule;
use App\Http\Controllers\MessagePacket;
use App\Events\ClassInEvent;
use App\Model\Eeoapi\Eeoschedule;


/**
 * 处理排课，支持约课，班课
 * Class ScheduleController
 * @package App\Http\Controllers\Business
 */
class  ScheduleController extends BaseController
{
    use MessagePacket;


    /** to be confirmed
     * 检查时间冲突
     * @param Request $request
     * @return array
     */
    function  check(Request $request)
    {
        # 参数检查
        if(!$request->has('checklist')){
            return $this->fail(false, '参数不全');
        }
        $checklist = $request->get('checklist');
        if(empty($checklist)) {
            return $this->fail(false, '参数不全:空对象');
        }

        foreach ($checklist as $key=>$item){
            $status = array("code"=>0,"message"=>"未发现冲突");
            $teacheruid =  $item['teacheruid'];
            $endtime = strtotime($item['endtime']);
            $begintime = strtotime($item['begintime']);
            $scheduleid = 0;
            if(isset($item['scheduleid'])){
                $scheduleid = $item['scheduleid'];
            }
            $beginday = date('Y-m-d 00:00:00', $begintime);
            $endday = date('Y-m-d 00:00:00', $begintime+86400);

            $config = SystemConfig::find(1)->pluck('checkteachertime')->first();
            if($config){
                # 查询教师时间
                $teachertimes = TimeStock::where('teacheruid', $teacheruid)
                    ->orderBy('begintime')
                    ->where(
                        function ($query) use($beginday, $endday)
                        {
                            $query->where('begintime', '>=', $beginday)
                                ->where('endtime', '<=', $endday);
                        })
                    ->get();
                # 查询时间重叠
                if(!empty($teachertimes[0])){
                    foreach ($teachertimes as $k=>$teachertime){
                        $begin = strtotime($teachertime->begintime);
                        $end = strtotime($teachertime->endtime);
                        if(isset($teachertimes[$k+1])){
                            if($teachertime->endtime >= $teachertimes[$k+1]->begintime){
                                $teachertimes[$k+1]->begintime = $teachertimes[$k]->begintime;
                            }
                        }
                    }
                }

                # 每条判断
                if(!empty($teachertimes[0])){

                    foreach($teachertimes as $value){
                        $status = array("code"=>2,"message"=>"该排课不在教师的可用时间内");
                        $end = strtotime($value->endtime);
                        $begin = strtotime($value->begintime);
                        if($begintime>$begin and $endtime<$end) {
                            $status = array("code"=>0,"message"=>"未发现冲突"); break;
                        }
                    }
                }else{
                    $status = array("code"=>2,"message"=>"该排课不在教师的可用时间内");
                }
            }
            # 查询
            $items = Schedule::where('teacheruid', $teacheruid)
                ->where('scheduleid','!=',$scheduleid)
                ->where(
                    function ($query) use($beginday, $endday)
                    {
                        $query->where('begintime', '>=', $beginday)
                            ->where('endtime', '<=', $endday);
                    })
                ->get();

            # 每条判断
            foreach($items as $time){
                $end = strtotime($time->endtime);
                $begin = strtotime($time->begintime);
                if($endtime>$begin and $endtime<$end) {
                    $status = array("code"=>1,"message"=>"教师已有排课时间冲突"); break;
                }
                if($begintime>$begin and $begintime<$end) {
                    $status = array("code"=>1,"message"=>"教师已有排课时间冲突"); break;
                }
                if($begintime<=$begin and $endtime>=$end) {
                    $status = array("code"=>1,"message"=>"教师已有排课时间冲突"); break;
                }
            }
            $checklist[$key]['status'] = $status;
        }
        # 返回成功
        return $this->success($checklist);
    }

    /**
     * 获取单一排课的信息
     * @param $id
     * @return packetMessage
     */
    function getSchedule($id)
    {
        $id = intval($id);
        $schedule = Schedule::find($id);
        if(empty($schedule)){
            return $this->fail($schedule, '数据不存在');
        }
        $scheduleStudent = ScheduleStuden::getStudentByScheduleID($schedule->scheduleid);
        $schedule->students = $scheduleStudent;
        $schedule->castinfo = array(
            "live"=>$schedule->live,
            "record"=>$schedule->record,
            "replay"=>$schedule->replay
        );
        unset($schedule->live,$schedule->record,$schedule->replay);
        return $this->success($schedule);
    }

    /**
     * 获取课节行为数据信息
     * @param $id
     * @return packetMessage
     */
    function getEeoScheduleInfo(Request $request)
    {
        $fileds = $request->all();
        return $this->success($fileds);
    }


    /**
     * 批量查询排课
     * @param Request $request
     * @return packetMessage
     */
    function getSchedules(Request $request)
    {
        $schedule = Schedule::whereNotNull('type');
        # 约／未约
        if($request->has('status')){
            $status = $request->get('status');
            $schedule->where('status', $status);
        }
        # 按老师查询
        if($request->has('teacheruid')) {
            $tuid = intval($request->get('teacheruid'));
            $schedule->where('teacheruid', $tuid);
        }
        # 指定开始时间
        if($request->has('begintime')){
            $schedule->where('begintime', '>=', $request->get('begintime'));
        }
        # 指定结束时间
        if($request->has('endtime')){
            $schedule->where('endtime', '<', $request->get('endtime'));
        }
        $schedules = $schedule->orderBy('begintime')->get();

        #附加教师名字
        foreach ($schedules as $k=>$item){
            $teacher = Teacher::find($item->teacheruid);
            if($teacher){
              $schedules[$k]->teacherName = $teacher->lastname;
            }
        }

        if(empty($schedules)){
            return $this->fail($schedules, '数据不存在');
        }
        foreach ($schedules as $k=>$item){
            $scheduleStudent = ScheduleStuden::getStudentByScheduleID($item->scheduleid);
            $begin = date('H:i', strtotime($item->begintime));
            $end = date('H:i', strtotime($item->endtime));
            $schedules[$k]->period = "$begin - $end";
            $students = [];
            $auditors = [];
            foreach ($scheduleStudent as $student){
                if($student->identity==1){
                    $students[] = $student;
                }else{
                    $auditors[] = $student;
                }
            }
            $schedules[$k]->students = $students ;
            $schedules[$k]->auditors = $auditors ;
        }
        foreach ($schedules as $schedule){
            $schedule->castinfo = array(
                "live"=>$schedule->live,
                "record"=>$schedule->record,
                "replay"=>$schedule->replay,
            );
            $videosite = [];
            ## 获取课节回放视频
            $result = DB::table('schedules_videos')
                ->select('videosite')
                ->where('scheduleid',$schedule->scheduleid)
                ->get();
            foreach ($result as $k=>$v) {
                $videosite[] = $v->videosite;
            }
            $schedule->videosite = $videosite;
            unset($schedule->live,$schedule->record,$schedule->replay);
        }


        return $this->success($schedules);
    }

    /**
     * 获取学生课表
     */
    function getSchedulesStudents(Request $request)
    {
        $name = '';
        $mobile = '';
        if($request->has('studentname')){
            $name = $request->get('studentname');
        }
        if($request->has('studentmobile')){
            $mobile = $request->get('studentmobile');
        }

        $result = ScheduleStuden::getScheduleStudents($name, $mobile , $request);
        return $this->success($result);
    }


    /**
     * 获取可用的老师
     */
    function getSchedusleTeachers(Request $request)
    {
        if($request->has('endtime')){
            $request->endtime = date('Y-m-d',strtotime($request->endtime)+24*60*60);
        }

        # 获取老师
        $items= $this->getValidTeachers($request);

        if(empty($items)){
            return $this->fail($items,'没还数据');
        }
        list($begintime, $endtime, $courseType, $levels) = $this->getTime($request);

        $teachers = [];
        foreach ($items as $item){
            $teacher =  $this->getTeacherSchedules($item->teacheruid, $begintime, $endtime, $courseType, $levels);
            if(empty($teacher)) {
                continue;
            }
            $teachers[] = $teacher;
        }

        if($teachers){
            return $this->success($teachers);
        }else{
            return $this->fail($teachers);
        }
    }


    /**
     * 获取某老师的可约课时间
     */
    function getSchedulesTeachertimes(Request $request)
    {
        if($request->has('teacherid')){
            $teacherid = $request->teacherid;
        }else{
            return $this->fail(null, '参数不全：缺少教师ID');
        }
        # 获取可约课节
        $items = $this->getValidTeacherSchedules($request);

        # 获取教师信息
        $teacher = Teacher::find($teacherid);
        $infos['days'] = [];
        $infos['teacherid'] = $teacher->teacheruid;
        $infos['teachername'] = $teacher->lastname;

        #格式化返回数据结构
        foreach ($items as $item){
            if($item->quotas <= $item->usequotas)  continue;
            $info = [];
            $time = strtotime($item->begintime);
            $day = date('Y-m-d', $time);

            # 不存在，创建一个日期项
            if(!key_exists($day, $infos['days'])){
                $info['list'] = [];
                $info['day'] = $day;
                $info['week'] = date('l', $time);
                $infos['days'][$day] = $info;
            }
            $item->quotas -= $item->usequotas;
            unset($item->usequotas);
            $infos['days'][$day]['list'][] = $item;
        }

        return $this->success($infos);
    }


    /**
     * 添加一个排课
     * @param Request $request
     * @return packetMessage
     */
    function  addSchedule(Request $request)
    {
        
            $auditors = [];
            $fields = $request->all();
            if ($request->has('auditors')) {
                $auditors = $request->get('auditors');
                unset($fields['auditors']);
            }
            # 开课时间控制
            $now = time() + 60;
            $begintime = strtotime($fields['begintime']);
            if ($begintime <= $now) {
                return $this->fail(false, '开课时间至少一分钟以后');
            }
            if (empty($fields['teacheruid'])) {
                return $this->fail(false, '缺少教师');
            }
            # 自动添加类型
            if ($request->has('type')) {
                $fields['type'] = $request->type;
            } else {
                # 不填，默认是约课
                $fields['type'] = Schedule::APPOINT;
            }
            # 直播参数
            if(key_exists('castinfo',$fields)){
                $castInfo = $fields['castinfo'];
                unset($fields['castinfo']);
                foreach ($castInfo as $k=>$v){
                    $fields[$k] = $v;
                }
            }
            $schedule = new Schedule($fields);
            $schedule->created_by = Token::user()->userid;
            $schedule->updated_by = Token::user()->userid;
            $schedule->save();
            # 添加旁听生
            foreach ($auditors as $auditor) {
                $sst = new ScheduleStuden();
                $sst->scheduleid = $schedule->scheduleid;
                $sst->identity = ClasseStuden::AUDITOR;
                $sst->studentuid = $auditor;
                $sst->save();
            }
        try {
            # 抛出添加事件
            if ($schedule->type == Schedule::APPOINT) {
                $this->dispatchAppointEvent($schedule, Event::CREATE, $auditors);
            }
            return $this->success($schedule);

        }catch (\Exception $e){
            $this->deleteSchedule($schedule->scheduleid);
            return $this->fail(false, '保存失败'.$e->getMessage());
        }
    }


    /**
     * 更新某个排课的信息
     * @param Request $request
     * @return packetMessage
     */
    function  updateSchedule(Request $request, $id=null)
    {
        try {
            ## 查找课节信息
            $schedule = Schedule::find($id);
            $oldTimes = $schedule->realSchedules();
            $fields = $request->all();
            $auditors = [];
            if ($request->has('auditors')) {
                $auditors = $request->get('auditors');
                unset($fields['auditors']);
            }
            if(key_exists('castinfo',$fields)){
                $castInfo = $fields['castinfo'];
                unset($fields['castinfo']);
                foreach ($castInfo as $k=>$v){
                    $schedule->$k = $v;
                }

                $this->dispatchScheduleEvent($schedule, Event::LIVE);

                $schedule->updated_by = Token::user()->userid;

                $schedule->save();

            }

            if($schedule->begintime < date('Y-m-d H:i:s')){
                return $this->fail(false, '修改失败，该课节正在上课中或已结束,只可修改课节回放状态');
            }

            if($fields['begintime'] < date('Y-m-d H:i:s',time()+60)){
                return $this->fail(false, '修改失败，开课时间至少一分钟以后');
            }



            $auditors_old = ScheduleStuden::where('scheduleid',$schedule->scheduleid)
                ->where('identity',2)
                ->get();

            # 计算旁听生变更
            $auditors_add = [];
            if(is_array($auditors)){
                foreach ($auditors as $auditor) {
                    $isnew = 1;
                    foreach($auditors_old as $auditor_old){
                        if($auditor == $auditor_old->studentuid){
                            $isnew = 0;
                        }
                    }
                    if($isnew){
                        $auditors_add[] = $auditor;
                    }
                }
            }
            $auditors_del = [];
            foreach ($auditors_old as $auditor_old){
                $isnew = 1;
                foreach ($auditors as $auditor){
                    if($auditor_old->studentuid == $auditor){
                        $isnew = 0;
                    }
                }
                if($isnew){
                    $auditors_del[] = $auditor_old->studentuid;
                }
            }

            foreach ($fields as $key=>$value){
                $schedule->$key = $value;
            }

            # 抛出更新事件
            if ($schedule->type == Schedule::APPOINT) {
                $this->dispatchAppointEvent($schedule, Event::UPDATE, $auditors_add, $auditors_del);
            }
            else{
                $this->dispatchScheduleEvent($schedule, Event::UPDATE);
            }


            $schedule->update($fields);

            $newTimes = $schedule->realSchedules();

            # 时间变化，更新课次统计
            $realTimes = $newTimes - $oldTimes;
            if($realTimes<>0){
                $asts = AppointmentSchedule::where('scheduleid', $schedule->scheduleid)->get();
                if(is_array($asts)){
                    foreach ($asts as $ast){
                        $app = Appointment::find($ast->appointid);
                        $app->appoint($realTimes);
                    }
                }
            }

            # 更新试听预约课
            Demoment::where('scheduleid', $schedule->scheduleid)
                ->update(['begintime' => $schedule->begintime, 'endtime' => $schedule->endtime]);

            # 更新正式预约课
            AppointmentSchedule::where('scheduleid', $schedule->scheduleid)
                ->update(['begintime' => $schedule->begintime, 'endtime' => $schedule->endtime,
                    'coursetypeid' => $schedule->coursetypeid, 'coursetype' => $schedule->coursetypename]);

            # 清掉旁听
            ScheduleStuden::where('identity', ClasseStuden::AUDITOR)
                ->where('scheduleid', $schedule->scheduleid)
                ->delete();
            # 添加旁听
            if(is_array($auditors)){
                foreach ($auditors as $auditor) {
                    $sst = new ScheduleStuden();
                    $sst->scheduleid = $schedule->scheduleid;
                    $sst->identity = ClasseStuden::AUDITOR;
                    $sst->studentuid = $auditor;
                    $sst->save();
                }
            }
            # 返回结果
            return $this->success($schedule);

        }catch (\Exception $e) {
            return $this->fail(false, '保存失败' . $e->getMessage());
        }
    }


    /**
     * 删除某个时间
     * @param $id
     * @return packetMessage
     * @throws
     */
    function  deleteSchedule($id)
    {
        try {
            $obj = Schedule::find($id);

            #抛出事件
            if ($obj->type == Schedule::APPOINT) {
                $this->dispatchAppointEvent($obj, Event::DELETE);
            } else {
                $this->dispatchScheduleEvent($obj, Event::DELETE);
            }

            # 清除试听课约课
            $sets = ['begintime' => null, 'endtime' => null, 'scheduleid' => null, 'status' => 0, 'teachername' => null];
            Demoment::where('scheduleid', $id)->update($sets);

            # 清除正式课约课
            $appSchedules = AppointmentSchedule::where('scheduleid', $id)->get();
            if ($appSchedules) {
                foreach ($appSchedules as $appSchedule) {
                    $appoint = Appointment::find($appSchedule->appointid);
                    if ($appoint) {
                        $appoint->unppoint($obj->realSchedules());
                    }
                }
                AppointmentSchedule::where('scheduleid', $id)->delete();
            }
            # 清除学生关系
            ScheduleStuden::where('scheduleid', $obj->scheduleid)->delete();
            # 删除记录
            Schedule::destroy($id);

            return $this->success(true);
        } catch (\Exception $e) {
            return $this->fail(false, '删除失败' . $e->getMessage());
        }
    }

    /**
     * @param Request $request
     * @return array
     */
    protected function getTime(Request $request)
    {
        $levels = [];
        $endtime = '';
        $begintime = '';
        $coursetypeid = '';
        if(!$request->has('begintime')){
            $begintime = date('Y-m-d');
        }else{
            $begintime = $request->begintime;
        }
        if(!$request->has('endtime')){
            $endtime = date('Y-m-d', time()+86400*30);
        }else{
            $endtime = $request->endtime;
        }
        if($request->has('coursetypeid')){
            $coursetypeid = $request->coursetypeid;
        }
        if($request->has('levels')){
            $levels = $request->levels;
        }
        return [$begintime, $endtime, $coursetypeid, $levels];
    }

    /**
     * 获取可用老师
     * @param Request $request
     * @return mixed
     */
    protected function getValidTeachers(Request $request)
    {
        list($begintime, $endtime, $courseTypeid, $levels) = $this->getTime($request);
        $now = time()+60;
        $begin = strtotime($begintime);
        if($begin>$now){
            $theBeginTime = $begintime;
        }else{
            $theBeginTime = date('Y-m-d H:i:s');
        }
        # 查询
        $Obj  = Schedule::select('teacheruid')
            ->where('type', Schedule::APPOINT)
            ->where('endtime', '<', $endtime)
            ->where('begintime', '>=', $theBeginTime)
            ->groupBy('teacheruid');
        # levelid 过滤
        if(!empty($levels)){
            $Obj->whereIn('levelid',  $levels);
        }
        if(!empty($courseTypeid)){
            $Obj->where('coursetypeid',  $courseTypeid);
        }
        $items = $Obj->get();

        return $items;
    }

    /**
     * 获取约课教师信息
     * @param $teacherid
     * @param $begintime
     * @param $endtime
     * @param $courseTypeid
     * @return array
     */
    protected function getTeacherSchedules($teacherid, $begintime, $endtime, $courseTypeid, $levels)
    {
        $Tobj = Teacher::find($teacherid);
        $teacher = ['begintime' => 0, 'endtime' => 0, 'times' => 0];
        $teacher['teacherid'] = $Tobj->teacheruid;
        $teacher['teachername'] = $Tobj->lastname;

        # 计算最新时间
        $now = time()+60;
        $begin = strtotime($begintime);
        if($begin>$now){
            $theBeginTime = $begintime;
        }else{
            $theBeginTime = date('Y-m-d H:i:s');
        }


        # 查询数据
        $Obj = Schedule::select('teacheruid', 'begintime', 'quotas', 'usequotas')
            # 只读取约课排期
            ->where('type', Schedule::APPOINT)
            ->where('teacheruid', $teacherid)
            ->where('endtime', '<', $endtime)
            ->where('begintime', '>=', $theBeginTime);

        if (!empty($levels)) {
            $Obj->whereIn('levelid', $levels);
        }
        if (!empty($courseTypeid)) {
            $Obj->where('coursetypeid', $courseTypeid);
        }
        $items = $Obj->get();

        foreach ($items as $item) {
            # 跳过被占用光的
            if ($item->usequotas >= $item->quotas) continue;
            # 计算可用时间
            $teacher['times'] += $item->quotas - $item->usequotas;
            # 设置开始日期
            if ($teacher['begintime'] == 0) $teacher['begintime'] = date('Y-m-d', strtotime($item->begintime));
            if (strtotime($item->begintime) <= strtotime($teacher['begintime'])) {
                $teacher['begintime'] = date('Y-m-d', strtotime($item->begintime));
            }
            # 设置结束日期
            if ($teacher['endtime'] == 0) $teacher['endtime'] = $item->begintime;
            if (strtotime($item->begintime) >= strtotime($teacher['endtime'])) {
                $teacher['endtime'] = date('Y-m-d', strtotime($item->begintime));
            }
        }
        if ($teacher['times'] <= 0) {
            $teacher = [];
        }
        return $teacher;
    }


    /**
     * 获取教师的可约课节
     * @param Request $request
     * @return mixed
     */
    protected function getValidTeacherSchedules(Request $request)
    {
        # 初始化查询条件
        $teacherid = $request->teacherid;

        $now = time()+60;
        $begin = strtotime($request->begintime);
        if($begin>=$now){
            $theBeginTime = $begin;
        }else{
            $theBeginTime = $now;
        }

        $begintime = date('Y-m-d H:i:s', $theBeginTime);
        $endtime = date('Y-m-d H:i:s', strtotime($request->endtime)+86400);

        # 查询
        $fields = ['scheduleid','name', 'begintime', 'endtime', 'quotas', 'usequotas', 'coursetypename'];
        $Obj = Schedule::select($fields)
            ->where('type', Schedule::APPOINT)
            ->where('teacheruid', $teacherid)
            ->where('endtime', '<=', $endtime)
            ->where('begintime', '>=', $begintime);

        if($request->has('levels')){
            $Obj->whereIn('levelid',  $request->levels);
        }
        if($request->has('coursetypeid')){
            $Obj->where('coursetypeid',  $request->coursetypeid);
        }
        $items = $Obj->get();

        return $items;
    }


    /**
     * web 唤醒
     * @param Request $request
     * @param $id
     * @throws \Exception
     * @return array
     */
    function wakeupClassIn(Request $request, $id)
    {
        #todo 检查学生，教师是不属于某个课节

        try{
            if(!$request->has('type')){
                # type[ 2:老师，3:学生 ]
                throw  new \Exception('缺少人员类型: type');
            }
            if(!$request->has('userid')){
                throw  new \Exception('缺少学生ID: userid');
            }
            #设置ClassInNo
            $classInNo = '';
            $userid = $request->get('userid');
            if($request->get('type')==3){
                $obj = Student::where('userid', $userid)->first();
            }elseif ($request->get('type')==2){
                $obj = Teacher::where('userid', $userid)->first();
            }
            $classInNo = $obj->ClassInNo;
            $eeo = Eeoschedule::where('scheduleid', $id)->first();

            $event = new ClassInEvent(ClassInEvent::WAKEUP, $classInNo, $eeo->classId, $eeo->courseId);
            event($event);
            return $this->success($event->getResult());

        }catch (\Exception $e){
            return $this->fail(false, $e->getMessage());
        }
    }



    /**
     * 获取学生课表
     * @param Request $request
     * @param $id
     * @return
     */
    function getStudentSchedule(Request $request, $id)
    {
        if($request->has('endtime')){
            $end = $request->get('endtime');
        }
        if($request->has('begintime')){
            $begin = $request->get('begintime');
        }
        $scheduleids = ScheduleStuden::where('studentuid', $id)->pluck('scheduleid')->toarray();
        $query = Schedule::whereIn('scheduleid', $scheduleids);
        if(isset($begin)){
            $query->where('begintime','>=', $begin);
        }
        if(isset($end)){
            $query->where('endtime','<', $end);
        }
        $schedules =$query->orderBy('begintime')->get();
        foreach ($schedules as $k=>$item){
            $teacher = Teacher::find($item->teacheruid);
            if($teacher){
                $schedules[$k]->teacherName = $teacher->lastname;
            }
            $begin = date('H:i', strtotime($item->begintime));
            $end = date('H:i', strtotime($item->endtime));
            $schedules[$k]->period = "$begin - $end";
        }
        return $this->success($schedules);
    }

    /**
     * 抛出事件
     * @param $action
     * @param $obj
     * @return mixed
     * @throws \Exception
     */
    protected function dispatchAppointEvent($obj, $action, $auditors=null,$auditors_del = null)
    {
        try{
            $event = new AppointEvent($obj, $action);
            $event->setLesson($obj, $action);
            if(is_array($auditors)){
                $students = Student::find($auditors);
                $event->setIdentity(ClasseStuden::AUDITOR);
                $event->setStudents($students);
            }
            if(is_array($auditors_del)){
                $students = Student::find($auditors_del);
                $event->setIdentity(ClasseStuden::AUDITOR);
                $event->setStudents($students,true);
            }
            event($event);
        }catch (\Exception $e){
            throw $e;
            return $e->getMessage();
        }
    }

    /**
     * 抛出事件
     * @param $action
     * @param $obj
     * @return mixed
     * @throws \Exception
     */
    protected function dispatchScheduleEvent($obj, $action)
    {
        try{
            $event = new ScheduleEvent($obj, $action);
            event($event);
        }catch (\Exception $e){
            throw $e;
            return $e->getMessage();
        }
    }
}
